#pragma once
#include "stdafx.h"
#include <Xmemdc.h>
#include <DrawFunc.h>
#include <xdll.idl>
#include "XPngButton.h"

#define DEFAULT_BUTTON_COUNT	10
#define ID_CUSTOM_CLOSE_BUTTON	65535


class CXTitlebar : public CWindowImpl<CXTitlebar>
{
public:
	CXTitlebar(void) : m_pIconPng(NULL)
	{
		for (int i = 0; i < DEFAULT_BUTTON_COUNT; ++i)
		{
			m_pButtons[i] = NULL;
		}
		m_RectOffset.SetRectEmpty();
	}

	~CXTitlebar(void)
	{
		for (int i = 0; i < DEFAULT_BUTTON_COUNT; ++i)
		{
			if (m_pButtons[i] != NULL)
			{
				delete m_pButtons[i];
				m_pButtons[i] = NULL;
			}
		}

		if (m_pIconPng != NULL)
		{
			DeletePngObject(m_pIconPng);
			m_pIconPng = NULL;
		}
	}

	BEGIN_MSG_MAP_EX(CXTitlebar)
		MSG_WM_PAINT(OnPaint)
		MSG_WM_ERASEBKGND(OnEraseBkgnd)
		MSG_WM_COMMAND(OnCommand)
	END_MSG_MAP()

	LRESULT OnCommand(UINT , int nMsg, HWND hWnd)
	{
		::PostMessage(GetParent().m_hWnd, WM_COMMAND, nMsg, 0);
		return DefWindowProc();
	}



	/**************  command   ******************/
	LRESULT OnEraseBkgnd(HDC)
	{
		return TRUE;
	}
	LRESULT OnPaint(HDC hdc)
	{
		CPaintDC dc(m_hWnd);
		CRect rect;
		GetClientRect(&rect);

		CXMemDC memDC(dc.m_hDC, rect);
		//	::PatBlt(memDC.m_hDC, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, WHITENESS);

		CWindow parentWnd = GetParent();
		CRect parentRect(rect);
		ClientToScreen(parentRect);
		parentWnd.ScreenToClient(&parentRect);

		HDC parentDC = ::GetDC(parentWnd.m_hWnd);	
		::BitBlt(memDC.m_hDC, 0, 0,  parentRect.Width(), parentRect.Height(), parentDC, parentRect.left, parentRect.top, SRCCOPY);
		ReleaseDC(parentDC);

		if (m_pIconPng != NULL)
		{
			m_pIconPng->DrawPng(memDC.m_hDC, rect, 0, 0);
		}

		if (!m_strTitle.IsEmpty())
		{
			CRect rectTitle(rect);
			rectTitle.left += 5;
			DrawMyText(memDC.m_hDC, m_strTitle, rectTitle, m_colorTitle, DT_LEFT | DT_VCENTER | DT_SINGLELINE, DMT_BOLD);
		}

		return 0;
	}


	/****************   function   *****************************/

	void AddButton(UINT nID, const CString & strPng, int nButtonState, int nRightOffset = 0, CString strText = "", CString strTooltipText = "")
	{
		int nIndex = 0;
		for (nIndex = 0; nIndex < DEFAULT_BUTTON_COUNT; ++nIndex)
		{
			if (!m_pButtons[nIndex])
			{
				break;
			}
		}
		if (nIndex == DEFAULT_BUTTON_COUNT)
		{
			return;
		}

		CWindow Win = ::GetDlgItem(GetParent().m_hWnd, nID);
		m_pButtons[nIndex] = new CXPngButton();
		if (Win.m_hWnd)
		{	
			m_pButtons[nIndex]->SubclassWindow(Win.m_hWnd);
		}
		else
		{
			Win.m_hWnd = m_pButtons[nIndex]->Create(m_hWnd);
			m_pButtons[nIndex]->SetDlgCtrlID(nID);
		}
		m_pButtons[nIndex]->SetButtonPng(strPng, nButtonState);

		CRect rect = m_pButtons[nIndex]->GetRect();
		GetParent().ScreenToClient(&rect);
		if (nIndex)
		{
			rect.OffsetRect(m_RectOffset.left - rect.right + nRightOffset, 0);
		}
		else
		{
			CRect rectTitle;
			GetWindowRect(&rectTitle);
			GetParent().ScreenToClient(&rectTitle);
			int cx = rect.Width();
			rect.right = rectTitle.right - 5;
			rect.left = rect.right - cx;
		}

		::MoveWindow(Win.m_hWnd, rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
		m_RectOffset = rect;
	}

	void SetIconPng(const CString & strPng)
	{
		if (!::PathFileExists(strPng))
		{
			return;
		}

		if (m_pIconPng != NULL)
		{
			DeletePngObject(m_pIconPng);
			m_pIconPng = NULL;
		}

		m_pIconPng = NewPngObject();
		if (m_pIconPng != NULL)
		{
			m_pIconPng->LoadPngFile(strPng);
		}

	}

	void SetTitleText(const CString & str, COLORREF color){m_strTitle = str;m_colorTitle = color;};

private:
	CXPngButton *	m_pButtons[DEFAULT_BUTTON_COUNT];
	CRect 			m_RectOffset;
	CString			m_strTitle;
	COLORREF		m_colorTitle;
	IXpng		*	m_pIconPng;
};
